<?php

namespace App\Service\ServiceTrait;

use App\Model\User;
use App\Model\UserProfile;
use App\Model\UserSetting;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Utils\Context;

trait GetUserInfoTrait
{
    /**
     * 获取用户本身信息-单例
     * @param $userId
     * @return false|Builder|Model|mixed|object|null
     */
    protected function getUserInfo($userId)
    {
        $key = 'user_id:' . $userId;
        if (Context::has($key)) {
            return Context::get($key, false);
        }

        $userInfo = UserProfile::getProfile($userId);
        if (!$userInfo) {
            return false;
        }

        Context::set($key, $userInfo);

        return $userInfo;
    }

    protected function packHostSetting(&$ret = [], $hostProfile = null)
    {
        if (!User::checkValidVersion($this->curVersion, $this->appId, ['1.0.0'])) return;
        $setting = UserSetting::getShowPermit($hostProfile->user_id);
        //是否显示魅力值
        $ret['show_charm'] = $setting['show_charm'];
        //是否显示礼物墙
        $ret['show_gift'] = $setting['show_gift'];
        //是否显示位置
        $ret['show_location'] = $setting['show_location'];
    }
}